Skip to content

Use last element as QuickSort pivot#2185

Open
natecl wants to merge 3 commits into
trekhleb:masterfrom
natecl:codex/quick-sort-last-pivot-265
Open

Use last element as QuickSort pivot#2185
natecl wants to merge 3 commits into
trekhleb:masterfrom
natecl:codex/quick-sort-last-pivot-265

Conversation

@natecl

@natecl natecl commented Jun 16, 2026

Copy link
Copy Markdown

Summary

  • Use the last element as the QuickSort pivot via array.pop() instead of using array.shift() from the front of the array.
  • Preserve stable sorting by appending the popped pivot after equal elements that appeared before it.
  • Update the expected visit count for the not-sorted fixture from 62 to 85.

Closes #265.

Verification

  • npx jest src/algorithms/sorting/quick-sort/__test__/QuickSort.test.js --runInBand
  • npx eslint src/algorithms/sorting/quick-sort/QuickSort.js src/algorithms/sorting/quick-sort/__test__/QuickSort.test.js

Note

Related: #171 proposes a broader median-of-three QuickSort pivot change and also touches Kruskal. This PR is intentionally narrower and only addresses the shift() pivot cost described in #265.

@natecl natecl marked this pull request as ready for review June 16, 2026 20:01

@daltino daltino left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change to use the last element as the pivot in QuickSort seems reasonable, but it impacts the visiting count for the NOT_SORTED_ARRAY. The updated test ensures correctness, but the change to the visiting count requires verification to confirm it reflects the intended behavior. Also, double-check if these changes affect other test cases and the algorithm's performance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

QuickSort : Instead of taking pivot as 1st element, we should take the pivot as last element, due to time complexity

2 participants